package com.base.helper;

import com.base.comm.CommUrls;
import com.base.po.param.JH;
import com.base.utils.GsonUtil;
import com.base.utils.Redis.RedisUtil;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;

/**
 * 佰城宜卡久话接口帮助类
 *
 * @author Administrator
 *
 */
@Component
public class BC_JHHelper {
	public static BigDecimal JH = new BigDecimal("0.06");

	private final RedisUtil redisUtil;

	public BC_JHHelper(RedisUtil redisUtil) {
		this.redisUtil = redisUtil;
	}

	/**
	 * 本地获取TOKEN 调用运营商授权
	 *
	 * @return
	 */
	public String getRedis() {
		@SuppressWarnings("resource")
//		Jedis jedis = new Jedis(CommUrls.REDIS_HOST); // 连接到Redis服务器
//		jedis.auth(CommUrls.REDIS_POSSWORD); // 设置密码
//		String token = jedis.get(CommUrls.REDIS_JH_TOKEN_BC);
		String token = redisUtil.get(CommUrls.REDIS_JH_TOKEN_BC);
		System.out.println("直接获取redis token：" + token);
		if (token == null) {
			return getToken();
		}
		return token;
	}

	/**
	 * 从久话服务端获取TOKEN 调用运营商授权
	 *
	 * @return
	 */
	@SuppressWarnings("resource")
	public String getToken() {
		String tokendata = "{\"Username\":\"20543\",\"Password\":\"bOoPhlebWRBqg4Nivq2A1DERjgM9LgFd\"}";
		String tokenresult = WebHelper.PostData("http://118.31.238.194:8088/api/login", tokendata, "application/json",
				null);
		System.out.println(tokenresult);
		JH jh = GsonUtil.getObject(tokenresult, JH.class);
//		Jedis jedis = new Jedis(CommUrls.REDIS_HOST); // 连接到Redis服务器
//		jedis.set(CommUrls.REDIS_JH_TOKEN_BC, jh.getToken());// 更新token
		redisUtil.set(CommUrls.REDIS_JH_TOKEN_BC, jh.getToken());
		System.out.println("更新redis token：" + jh.getToken());
		return jh.getToken();
	}

	/**
	 * 拨打电话 (久话)
	 *
	 * @param caller 主叫
	 * @param callee164 被叫
	 * @param token
	 * @param id 随机UUID
	 * @return
	 */
	public static String bh(String caller, String callee164, String token, String id) {
		String datas = "{\"Callid\":\"" + id
				+ "\",\"App_id\":\"20543\",\"Appkey\":\"bOoPhlebWRBqg4Nivq2A1DERjgM9LgFd\",\"Caller\":\"" + caller
				+ "\",\"Callee\":\"" + callee164 + "\",\"Cdr_url\":\"" + CommUrls.THISSERVER + "call/JHpush" + "\"}";
		String callresult = WebHelper.PostData("http://118.31.238.194:8088/api/CallRequest", datas,
				"application/json", token);
		System.out.println(callresult);
		return callresult;
	}

	/**
	 * 拨打电话（魅逸）
	 *
	 * @param caller 主叫
	 * @param callee164 被叫
	 * @param id
	 * @param time
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String bhMy(String caller, String callee164, String id, int time) throws UnsupportedEncodingException {
		String callresult = WebHelper.GetData("http://api2.gz-tx.com:2015/index.html?zj=" + caller + "&bj=" + callee164
				+ "&calltime=" + time + "&pwd=c8837b23ff8aaa8a2dde915473ce0991&name=gztx695&params=" + id + "", null);
		return callresult;
	}

}
